/**
 * jims
 */
package com.jims.master.modules.csm.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.csm.entity.ExpBuyPlan;
import com.jims.master.modules.csm.service.ExpBuyPlanService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 消耗品采购计划查询Controller
 * @author DT
 * @version 2017-03-22
 */
@Controller
@RequestMapping(value = "${adminPath}/csm/expPlanCheck")
public class ExpPlanCheckController extends BaseController {

	@Autowired
	private ExpBuyPlanService expBuyPlanService;

	@ModelAttribute
	public ExpBuyPlan get(@RequestParam(required=false) String id) {
		ExpBuyPlan entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = expBuyPlanService.get(id);
		}
		if (entity == null){
			entity = new ExpBuyPlan();
		}
		return entity;
	}

	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(ExpBuyPlan expBuyPlan, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ExpBuyPlan> page = new Page<>();
        if(null != expBuyPlan && null != expBuyPlan.getPurchaseNumber()){
            List<ExpBuyPlan> list = expBuyPlanService.findByNumber(expBuyPlan);
            page.setList(list);
            expBuyPlan.setPage(page);
        }else{
            page.setList(null);
        }
        model.addAttribute("page",page);
		return "modules/csm/expPlanCheckIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(ExpBuyPlan expBuyPlan, Model model) {
		model.addAttribute("expBuyPlan", expBuyPlan);
		return "modules/csm/expPlanCheckForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(ExpBuyPlan expBuyPlan, Model model) {
	    StringData data=new StringData();
		int i=expBuyPlanService.save(expBuyPlan);
        if(i>0){
            data.setCode("success");
            data.setData("保存成功");
        }else{
            data.setCode("error");
            data.setData("保存失败");
        }
        return data;
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(ExpBuyPlan expBuyPlan) {
	    StringData data=new StringData();
		int i=expBuyPlanService.delete(expBuyPlan);
        if(i>0){
            data.setCode("success");
            data.setData("删除成功");
        }else{
            data.setCode("error");
            data.setData("删除失败");
        }
		return data;
	}

}